package com.thy.utils;

import com.hospital.common.pojo.TAuitLog;
import com.thy.aop.LogAction;
import com.thy.aop.LogTargetType;
import com.thy.mapper.TAuitLogMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class AuitLogUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext context) {
        applicationContext = context;
    }

    /**
     * 进行日志更新
     * @param adminId
     * @param actionId
     * @param targetId
     * @param targetTypeId
     */
    //传入管理员id，行为id，目标id，目标类型id
    public static void log(Long adminId, Long actionId, Long targetId, Long targetTypeId) {
        TAuitLogMapper auitLogMapper = applicationContext.getBean(TAuitLogMapper.class);
        TAuitLog auitLog = new TAuitLog();
        auitLog.setAdminId(adminId);//管理员id
        Integer role = auitLogMapper.getRole(adminId);//获取管理员角色
        String r;
        if (role == 3){
            r = "版主";
        }else {
            r = "超管";
        }
        String adminName = auitLogMapper.getAdminName(adminId);//获取管理员名称
        auitLog.setActionId(actionId);//行为id
        LogAction action = LogAction.getById(adminId);
        String actionName;//行为名称
        if (action != null){
            actionName = action.getAction();
        }else {
            actionName = "未知行为";
        }
        auitLog.setTargetId(targetId);//目标id
        String targetName = auitLogMapper.getTargetName(targetId);//目标名称
        auitLog.setTargetTypeId(targetTypeId);//目标类型id
        LogTargetType targetType = LogTargetType.getById(targetTypeId);
        String targetTypeName;//目标类型名称
        if (targetType != null){
            targetTypeName = targetType.getType();
        }else {
            targetTypeName = "未知目标类型";
        }
        auitLog.setExecuteTime(LocalDateTime.now());//执行时间
        //什么角色什么人在什么时候对什么目标执行了什么行为
        String description = r + "-" + adminName + "在" + auitLog.getExecuteTime() + "对"
                + targetTypeName + "--" + targetName + "进行了" + actionName;
        auitLog.setDescription(description);//操作描述
        // 通过ApplicationContext获取Mapper并插入数据库
        auitLogMapper.insert(auitLog);
    }

    /**
     * 获取用户角色
     * @param userId
     * @return
     */
    public static Integer getRole(Long userId) {
        TAuitLogMapper auitLogMapper = applicationContext.getBean(TAuitLogMapper.class);
        Integer role = auitLogMapper.getRole(userId);
        if (role == null){
            return 1;
        }else if (role == 3){
            return 2;
        }else {
            return 3;
        }
    }

    /**
     * 板块增删改日志记录
     */
    public static void logSection(Long adminId,Long sectionId,String url) {
        TAuitLogMapper auitLogMapper = applicationContext.getBean(TAuitLogMapper.class);
        TAuitLog auitLog = new TAuitLog();
        auitLog.setAdminId(adminId);//管理员id
        String adminName = auitLogMapper.getAdminName(adminId);//获取管理员名称
        //行为id
        if (url.contains("add") || url.contains("新增")){
            //添加板块
            auitLog.setActionId(8L);
        }else if (url.contains("delete") || url.contains("删除")){
            //删除板块
            auitLog.setActionId(9L);
        }else if (url.contains("update") || url.contains("修改") || url.contains("编辑")){
            //修改板块
            auitLog.setActionId(10L);
        }else {
            //默认修改板块
            auitLog.setActionId(10L);
        }
        auitLog.setTargetId(sectionId);//目标id
        auitLog.setTargetTypeId(4L);//目标类型id
        LocalDateTime executeTime = LocalDateTime.now();
        auitLog.setExecuteTime(executeTime);//执行时间
        String sectionName = auitLogMapper.getSectionName(sectionId);
        //什么角色什么人在什么时候对什么目标执行了什么行为
        String description = LogTargetType.SUPER_ADMINISTRATOR.getType() + "-" + adminName + "在" + executeTime
                + "对" + LogTargetType.SECTION.getType() + "--" + sectionName + "进行了" + LogAction.getById(auitLog.getActionId()).getAction();
        auitLog.setDescription(description);
        auitLogMapper.insert(auitLog);
    }

}
